package list.model;

import list.control.MysqlLink;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by ZhipengLi.
 */
public class Tag extends MysqlLink {
    private static final String TABLE = "tag";

    private int id;
    private int user_id;
    private String name;
    private int count;

    public Tag(int id, int user_id, String name, int count) {
        this.id = id;
        this.user_id = user_id;
        this.name = name;
        this.count = count;
    }

    private static String condition(int user_id, int tag_id) {
        return equalsAnd(MysqlLink.toArray("user_id", "id"), MysqlLink.toArray(user_id, tag_id));
    }

    public static boolean insert(int user_id) {
        return !isExited(user_id, "NEW") && insert(TABLE, MysqlLink.toArray("user_id", "name"), MysqlLink.toArray(user_id, "NEW"));
    }

    public static boolean delete(int user_id, int tag_id) {
        return delete(TABLE, condition(user_id, tag_id));
    }

    public static boolean update(int user_id, int tag_id, String new_name) {
        return update(TABLE, "name", new_name, condition(user_id, tag_id));
    }

    public static Tag[] select(int user_id) {
        ResultSet resultSet = MysqlLink.select(TABLE, MysqlLink.ALL, MysqlLink.equal("user_id", user_id));
        if (null != resultSet) {
            ArrayList<Tag> tags = new ArrayList<>();
            try {
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    int count = resultSet.getInt("count");
                    tags.add(new Tag(id, user_id, name, count));
                }
                resultSet.close();
                if (0 < tags.size()) {
                    Tag[] output = new Tag[tags.size()];
                    tags.toArray(output);
                    return output;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static boolean increase(int user_id, int tag_id) {

        return update(TABLE, "count", (getCount(tag_id) + 1), condition(user_id, tag_id));
    }

    public static boolean decrease(int user_id, int tag_id) {
        return update(TABLE, "count", (getCount(tag_id) - 1), condition(user_id, tag_id));
    }

    private static boolean isExited(int user_id, String name) {
        return exist(TABLE, "user_id", equalsAnd(toArray("user_id", "name"), toArray(user_id, name)));
    }

    private static int getCount(int tag_id) {
        ResultSet resultSet = select(TABLE, "count", equal("id", tag_id));
        int count = 0;
        if (null != resultSet) {
            try {
                if (resultSet.next()) {
                    count = resultSet.getInt("count");
                }
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    public int getId() {
        return id;
    }

    public int getUser_id() {
        return user_id;
    }

    public String getName() {
        return name;
    }

    public int getCount() {
        return count;
    }
}
